function h = mk_rev_ir(t60, sr, density)
% function h = mk_rev_ir(t60, sr, density)
%
% Generates a pseudo-random FIR filter for use in synthesizing reverb.
% Note that the T60 is related to physical room parameters by:
% T60 = 0.161*V/(S*a) where V is volume in meters, S is the surface area, and
% a is the average absorbtion/Sabine of the wall material.
%
% INPUT:
%    t60     [num] - T60 time in seconds [1.0]
%    sr      [num] - Sample rate [8000]
%    density [num] - Density (per sec) of non-zero filter taps [sr/10]
% 
% OUTPUT:
%    h       [vec] - Calculated IR
%

% Copyright (C) 2010-2030 Graham Grindlay (grindlay@ee.columbia.edu)
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

if nargin < 1
    t60 = 1.0;
end
if nargin < 2
    sr = 8000;
end
if nargin < 3
    density = sr/10;
end

f_len = t60*sr; % filter length
p = density/sr; % prob of non-zero filter taps
h = floor((1+p)*rand(1,f_len)-p/2);

tau = t60/log(1000); % time constant (-60dB corresponds to 1/1000)
tvec = (0:length(h)-1)/sr; % time vec (sec)
env = exp(-tvec/tau);
h = h .* env;
